//
//  CSGXCheckPreviewFooter.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/22.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXCheckPreviewFooter.h"
#import "CSGXCommodityPickerBLL.h"

#import "TInventoryModel.h"

@interface CSGXCheckPreviewFooter ()

@property (nonatomic, weak) UILabel *lblUnitCount;
@property (nonatomic, weak) UILabel *lblCount;




@end

@implementation CSGXCheckPreviewFooter


+ (instancetype)footerViewWithCallback:(CallBack_CheckPreviewFooter)callback_checkPreFooter {
    id obj = [[self alloc] init];
    ((CSGXCheckPreviewFooter *)obj).callback_checkPreFooter = callback_checkPreFooter;
    return obj;
}




#pragma mark - Actions
- (void)btnCommit:(id)sender {
    
    if (self.callback_checkPreFooter) {
        self.callback_checkPreFooter();
    }
}


#pragma mark - methods

- (void)distributeWithDict:(NSDictionary *)dict {
    NSString *unitCountStr = dict[@"unitCountStr"];
    NSAttributedString *mStr = [self getUnitCountAttrStr:unitCountStr];
    self.lblUnitCount.attributedText = mStr;
    
    NSInteger checkedCount = [self getCheckCount];
    self.lblCount.attributedText = [self getCheckCountAttrStr:[NSString stringWithFormat:@"共盘点%lu产品", (long)checkedCount]];
}

- (NSDictionary *)getFooterDict {
    NSMutableArray *mArr = [NSMutableArray arrayWithArray:@[@0, @0, @0]];
    for (int i = 0; i < self.checkPreviewFooterList.count; i++) {
        NSArray *countArr = [self.checkPreviewFooterList[i].iInventoryCount componentsSeparatedByString:@","];
        
        mArr[0] = @([mArr[0] intValue] + [countArr[0] intValue]);
//        mArr[1] = @([mArr[0] intValue] + [countArr[0] intValue]);
        mArr[2] = @([mArr[2] intValue] + [countArr[1] intValue]);
    }
    
    NSString *unitCountStr = [NSString stringWithFormat:@"%@大%@中%@小", mArr[0], mArr[1], mArr[2]];
    
    return @{@"unitCountStr":unitCountStr};
}

- (NSAttributedString *)getUnitCountAttrStr:(NSString *)str {
    NSRange rangeBig = [str rangeOfString:@"大"];
    NSRange rangeMid = [str rangeOfString:@"中"];
    NSRange rangeSma = [str rangeOfString:@"小"];
    
    NSRange tarRangeBig = NSMakeRange(0, rangeBig.location);
    NSRange tarRangeMid = NSMakeRange(rangeBig.location + rangeBig.length, rangeMid.location - (rangeBig.location + rangeBig.length));
    NSRange tarRangeSma = NSMakeRange(rangeMid.location + rangeMid.length, rangeSma.location - (rangeMid.location + rangeMid.length));
    
    NSMutableAttributedString * mAttrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} range:tarRangeBig];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} range:tarRangeMid];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} range:tarRangeSma];
    
    return mAttrStr;
}

- (NSAttributedString *)getCheckCountAttrStr:(NSString *)str {
    NSMutableAttributedString *mAttr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]}];
    [mAttr addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20]} range:NSMakeRange(3, 1)];
    return mAttr;
}

- (NSAttributedString *)getDirectionLabalAttrStr {
    NSString *str = @"绿色：盘盈，红色：盘亏，蓝色：盘平，黑色：未盘点。";
    NSMutableAttributedString *mAttr = [[NSMutableAttributedString alloc] initWithString:str];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:NSMakeRange(0, 5)];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} range:NSMakeRange(6, 5)];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} range:NSMakeRange(12, 5)];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(18, 6)];
    
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} range:NSMakeRange(5, 1)];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} range:NSMakeRange(11, 1)];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} range:NSMakeRange(17, 1)];
    [mAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} range:NSMakeRange(24, 1)];
    
    return mAttr.copy;
}

- (NSInteger)getCheckCount {
    NSInteger count = 0;
    for (int i = 0; i < self.checkPreviewFooterList.count; i++) {
        if (_checkPreviewFooterList[i].iIsChecked != 0) {
            count ++;
        }
    }
    return count;
}


#pragma mark - setupUI
- (void)setupUI {
    UIView *vLine = [[UIView alloc] init];
    [self addSubview:vLine];
    
    vLine.backgroundColor = [UIColor grayColor];
    
    [vLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self).mas_offset(0);
        make.height.mas_offset(3);
    }];
    
    //lblUnitCount
    UILabel *lblUnitCount = [UILabel gx_labelWithText:@"" fontSize:12 color:[UIColor orangeColor]];
    [self addSubview:lblUnitCount];
    self.lblUnitCount = lblUnitCount;
    
    [lblUnitCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vLine.mas_bottom).mas_offset(5);
        make.left.equalTo(self).mas_offset(10);
    }];
    
    //btnCommit
    UIButton *btnCommit = [UIButton gx_textButton:@"提交" fontSize:18 normalColor:kColorWhite selectedColor:kColorWhite];
    [self addSubview:btnCommit];
    
    [btnCommit addTarget:self action:@selector(btnCommit:) forControlEvents:UIControlEventTouchUpInside];
    btnCommit.backgroundColor = kColorBar;
    
    [btnCommit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self).mas_offset(3);
        make.width.mas_offset(80);
    }];
    
    //lblCount
    UILabel *lblCount = [UILabel gx_labelWithText:@"" fontSize:13 color:[UIColor orangeColor]];
    [self addSubview:lblCount];
    self.lblCount = lblCount;
    
    [lblCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vLine.mas_bottom).mas_offset(5);
        make.right.equalTo(btnCommit.mas_left).mas_offset(-5);
    }];
    
    //
    UILabel *lblDirection = [UILabel gx_labelWithText:@"" fontSize:10 color:[UIColor blackColor]];
    [self addSubview:lblDirection];
    
    [lblDirection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-5);
        make.height.mas_offset(17);
    }];
    
    lblDirection.attributedText = [self getDirectionLabalAttrStr];

}

#pragma mark - setter & getter

- (void)setCheckPreviewFooterList:(NSArray<TInventoryModel *> *)checkPreviewFooterList {
    _checkPreviewFooterList = checkPreviewFooterList;
    NSDictionary *dict = [self getFooterDict];
    [self distributeWithDict:dict];
}





@end
